package wili.attrMapping._user.b1;

import java.util.List;

import wili.attrMapping._user.b1.exp.AndOrExp2;
import wili.attrMapping._user.b1.exp.AndOrNode2;
import wili.attrMapping.core.Att;
import wili.attrMapping.core.Atts;
import wili.attrMapping.core.AttsDefine;
import wili.attrMapping.core.AttsFile;
import wili.c.Ca;

/**
 * 
 * @author weilai2@163.com
 *
 */
public class JsonVisibleConf extends B1JsonUserConf {
	public JsonVisibleConf() {
		super("visible.txt", new JsonVisibleDefine(true, "guid", "visible", "visible"));
	}

	@Override
	public List<Atts> findOldAttsList(AttsDefine def, AttsFile file) {
		return super.findOldAttsList(def, file);
	}

	@Override
	public List<Atts> findNewAttsList(Atts oldAtts) {
		return super.findNewAttsList(oldAtts);
	}

	@Override
	public String genRuleOf_oldNewAtts(Atts oldAtts, Atts newAtts) {
		//仅凭一对新旧属性集实例，来猜测构造映射规则。
		String FORMULA = "formula", FORMULA2 = "@{formula}";
		Att oldAtt = oldAtts.att(FORMULA);
		String ruleValue = null;
		{//公式属性 ~>新值 ~>是否存在于新属性中? 把存在的部分换做占位符 ~>生成公式
			if (oldAtt == null || oldAtt.value.isEmpty()) {
				// 无公式
				ruleValue = newAtts.value;
			} else {// 概念：规则值，旧值，新值，新属性值 =新值+?
				// 旧值 ~>新值, (新值，新属性值)~>规则值
				// 规则值可能依靠猜测。eg. ("v1", "v1 && #a") ~> "@{fm} && #a"
				String oldValue2 = B1com.formularTransform(oldAtts, oldAtt.value, false);
				AndOrNode2 newAttsExp = AndOrExp2.newInst(-1, newAtts.value).rootNode;
				AndOrNode2 oldExp2 = AndOrExp2.newInst(-1, oldValue2).rootNode;
				newAttsExp.sortSonsBySt(9);
				oldExp2.sortSonsBySt(9);//
				if (newAttsExp.equals(oldExp2)) {
					// 新值=新属性值
					ruleValue = AndOrExp2.newInst(-1, FORMULA2).rootNode.getString(1);
				} else {
					//${ObjType} === 13 && ${DocType} === 'I' && ${VShowAssValDate} === 'Y'
					//${DocType} === 'I' && ${ObjType} === '13' && ${VShowAssValDate} === 'Y'
					int ind = AndOrNode2.indexOfExpNode(oldExp2, newAttsExp.sons);
					if (newAttsExp.isAnd != null && newAttsExp.isAnd && ind != -1) {// 表达式中可能局部出现多余括号，无法字串匹配
						// 新值属于新属性值： 旧值的新值在新属性中
						newAttsExp.sons.set(ind, AndOrExp2.newInst(-1, FORMULA2).rootNode);
						newAttsExp.sortSonsBySt(9);
						ruleValue = newAttsExp.getString(1);
					} else {
						ruleValue = newAttsExp.getString(1);
					}
					Ca.pause();
				}
				ruleValue = standValueLR(ruleValue);
			}
		}
		return ruleValue;
	}
}
